package com.gsx.Controller;

import com.github.pagehelper.PageInfo;
import com.gsx.entity.Blog_Type;
import com.gsx.entity.Comment;
import com.gsx.entity.UserInfo;
import com.gsx.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    @GetMapping ("/comments/{blog_id}")
        public String comments(@PathVariable Long blog_id, Model model){
        model.addAttribute("comments",commentService.getCommentListById(blog_id));
        return "blog :: commentList";
    }

    @PostMapping("/comments")
    public String post(Comment comment, HttpSession session){
        UserInfo userInfo=(UserInfo) session.getAttribute("user");
        comment.setComment_nickname(userInfo.getNickname());
        comment.setComment_avatar(userInfo.getAvatar());
        comment.setUser_id(userInfo.getUser_id());
        comment.setComment_email(userInfo.getEmail());
        commentService.saveComment(comment);
        return "redirect:/comments/"+comment.getBlog_id();
    }

    @GetMapping("/admain/comments")
    public String blogs(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                        @RequestParam(name="size",required = true,defaultValue = "10")int size, Model model,
                        Comment comment){
        PageInfo pageInfo =new PageInfo(commentService.getCommentByBack(page,size,comment));
        model.addAttribute("pageInfo",pageInfo);
        return "admain/comments";
    }
    /*根据内容查询*/
    @PostMapping("/admain/comments/search")
    public String getComments(@RequestParam(name="page",required = true,defaultValue = "0")int page,
                              @RequestParam(name="size",required = true,defaultValue = "10")int size, Model model,Comment comment){
        List<Comment> list=commentService.getCommentByBack(page,size,comment);
        PageInfo pageInfo=new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        return "admain/comments :: commentList";
    }

    @GetMapping("/admain/comments/{id}/delete")
    public String delBlog(RedirectAttributes attributes, @PathVariable Long id){
        int result=commentService.deleteComment(id);
        if(result==0){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admain/comments";
    }
}
